package com.guanghua.brick;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.biz.CommonLocator;
import com.guanghua.brick.biz.ILocator;
import com.guanghua.brick.db.CommonHBSessionFactory;
import com.guanghua.brick.db.IHBSessionFactory;
import com.guanghua.brick.db.ISQLConnection;
import com.guanghua.brick.db.ISQLFilterSection;
import com.guanghua.brick.db.ISQLPagerSection;
import com.guanghua.brick.db.OracleDefaultSQLFilterSection;
import com.guanghua.brick.db.OracleSQLPagerSection;
import com.guanghua.brick.db.PageInfo;
import com.guanghua.brick.db.URLSQLConnection;
import com.guanghua.brick.util.IConfig;
import com.guanghua.brick.util.XmlReader;

/**
 * brick app 框架需要的一些静态参数
 * @author leeon
 *
 */
public class BrickConstant {
	
	private static final Log logger = LogFactory.getLog(BrickConstant.class);
	
	private static String APP_DB_CONN = "com.guanghua.brick.db.URLSQLConnection";
	private static String APP_DB_HB_SF = "com.guanghua.brick.db.CommonHBSessionFactory";
	private static String APP_DB_PAGER = "com.guanghua.brick.db.OracleSQLPagerSection";
	private static String APP_DB_FILTER = "com.guanghua.brick.db.OracleDefaultSQLFilterSection";
	private static String APP_BIZ_CONTAINER = "com.guanghua.brick.biz.CommonLocator";
	
	
	public static IConfig APP_DB_CONN_CONFIG = null;
	public static IConfig APP_BIZ_CONTAINER_CONFIG = null;
	
	//读取配置文件初始化参数
	static {
		try {
			//读取配置
			XmlReader reader = new XmlReader("brick.app.xml");
			//sql数据库连接配置
			IConfig[] c = reader.readByTagName("app-db-conn");
			if (c != null && c.length != 0 && c[0] != null) {
				APP_DB_CONN = c[0].getAttribute("class");
				APP_DB_CONN_CONFIG = c[0];
			}
			
			//hb sf 实现的配置
			c = reader.readByTagName("app-hb-sf");
			if (c != null && c.length != 0 && c[0] != null)
				APP_DB_HB_SF = c[0].getText();
			
			//翻页解析配置
			c = reader.readByTagName("app-db-pager");
			if (c != null && c.length != 0 && c[0] != null) {
				APP_DB_PAGER = c[0].getText();
				if (c[0].getAttribute("defaultPageSize") != null)
					PageInfo.DEFAULT_PAGE_SIZE = Integer.parseInt(c[0].getAttribute("defaultPageSize"));
			}
			
			//filter解析配置
			c = reader.readByTagName("app-db-filter");
			if (c != null && c.length != 0 && c[0] != null)
				APP_DB_FILTER = c[0].getText();
			
			//业务方法容器配置
			c = reader.readByTagName("app-biz-container");
			if (c != null && c.length != 0 && c[0] != null) {
				APP_BIZ_CONTAINER = c[0].getAttribute("class");
				APP_BIZ_CONTAINER_CONFIG = c[0];
			}
			
		} catch (Exception e) {
			logger.error("parse xml brick.app.xml to init data error, use default", e);
		}
	}
	
	public static ISQLConnection getSQLConnection() {
		try {
			Object o = Class.forName(APP_DB_CONN).newInstance();
			return (ISQLConnection)o;
		} catch (Exception e) {
			return new URLSQLConnection();
		}
	}

	//返回ihbsf接口实现类，根据spring配置和普通配置两种
	public static IHBSessionFactory getHBSessionFactory() {
		try {
			Object o = Class.forName(APP_DB_HB_SF).newInstance();
			return (IHBSessionFactory)o;
		} catch (Exception e) {
			return new CommonHBSessionFactory();
		}
	}

	
	//返回sqlpagersection 接口的实现类，根据数据库类型不同，分页方法不同
	public static ISQLPagerSection getSQLPagerSection() {
		try {
			Object o = Class.forName(APP_DB_PAGER).newInstance();
			return (ISQLPagerSection)o;
		} catch (Exception e) {
			return new OracleSQLPagerSection();
		}
	}
	
	//返回sqlwheresection 接口的实现类，根据数据库不同及项目不同，filter section的翻译不同
	public static ISQLFilterSection getSQLFilterSection() {
		try {
			Object o = Class.forName(APP_DB_FILTER).newInstance();
			return (ISQLFilterSection)o;
		} catch (Exception e) {
			return new OracleDefaultSQLFilterSection();
		}
	}
	
	//返回delegate接口实现类，根据biz容器不同，实现不同
	public static ILocator getLocator() {
		try {
			Object o = Class.forName(APP_BIZ_CONTAINER).newInstance();
			return (ILocator)o;
		} catch (Exception e) {
			return new CommonLocator();
		}
	}
	
}
